import os
import sys
import chardet

g_curFolder = sys.path[0]+"\\"

# "E:\\Users\\yinxunqian\\Documents\\xcl_files\\Sql"
# "E:\\Users\\yinxunqian\\sql_Script"

g_targetPath = r"D:\Users\fredy\Desktop\Fred-all-work\!!数字中心\!proj-Xchain\!迁移脚本\用户-关系\mysql57-version\01dev"

g_targetPath_uat = r"D:\Users\fredy\Desktop\Fred-all-work\!!数字中心\!proj-Xchain\!迁移脚本\用户-关系\mysql57-version\02uat"

g_targetPath_prd = r"D:\Users\fredy\Desktop\Fred-all-work\!!数字中心\!proj-Xchain\!迁移脚本\用户-关系\mysql57-version\03prd"

g_basedata = r"D:\Users\fredy\Desktop\Fred-all-work\!!数字中心\!proj-Xchain\!迁移脚本\报价要素\dx-m-t-done - mysql\01dev"

g_basedata_uat = r"D:\Users\fredy\Desktop\Fred-all-work\!!数字中心\!proj-Xchain\!迁移脚本\报价要素\dx-m-t-done - mysql\02uat"

g_basedata_prd = r"D:\Users\fredy\Desktop\Fred-all-work\!!数字中心\!proj-Xchain\!迁移脚本\报价要素\dx-m-t-done - mysql\03prd"

def SearchPath(path, type):
    itemlist = os.listdir(path)
    for f in itemlist:
        fullpath = path + "/" + f
        if(os.path.isfile(fullpath)):
            #replace
            if(type == 0):
                Replace(fullpath)
            if(type == 1):
                ReplaceToUat(fullpath)
            if(type == 2):
                ReplaceToPrd(fullpath)    
            
        if(os.path.isdir(fullpath)):
            SearchPath(fullpath,type)

def Replace(path):
    encodingstr = GetEncode(path)
    file_content = ""
    with open(path, "r+", encoding=encodingstr,errors='ignore') as f :
        for l in f.readlines():
            if(":4000" in l):
                l = l.replace(":4000",":3306")
                print("replace-4000")
            if("tidbdev" in l):
                l = l.replace("tidbdev", "mysql57dev")
                print("replace-dev")
            if("tidbuat" in l):
                l = l.replace("tidbuat", "mysql57uat")
                print("replace-uat")
            file_content += l
    
    with open(path,"w",encoding=encodingstr,errors='ignore') as f2:
        f2.write(file_content)
        print("-- write")

def ReplaceToUat(path):
    encodingstr = GetEncode(path)
    file_content = ""
    with open(path, "r+", encoding=encodingstr,errors='ignore') as f :
        for l in f.readlines():
            if("mysql57dev" in l):
                l = l.replace("mysql57dev","mysql57uat")
                print("replace-dev-uat-acc")
            if("192.168.0.5:3306" in l):
                l = l.replace("192.168.0.5:3306","81.71.32.11:3306")
                print("replace-dev-uat-link")
            file_content += l
    
    with open(path,"w",encoding=encodingstr,errors='ignore') as f2:
        f2.write(file_content)
        print("-- write")

def ReplaceToPrd(path):
    encodingstr = GetEncode(path)
    file_content = ""
    with open(path, "r+", encoding=encodingstr,errors='ignore') as f :
        for l in f.readlines():
            # source
            if("120.79.30.100:1433;DatabaseName=GSKZUat" in l):
                l = l.replace("120.79.30.100:1433;DatabaseName=GSKZUat","8.135.132.74:1433;DatabaseName=GSKZPrd")
                print("replace-uat-prd-link")
            if("120.78.229.69:1433;DatabaseName=XiaoChiLunUat" in l):
                l = l.replace("120.78.229.69:1433;DatabaseName=XiaoChiLunUat","39.108.118.85:1433;DatabaseName=XiaoChiLunPrd")
                print("replace-uat-prd-link-xcl")
            if("db.30.100!" in l):
                l = l.replace("db.30.100!","Gskz@mssql-2019")
                print("replace-uat-prd-pwd")
            if("Welcome1!4@ygcDB" in l):
                l = l.replace("Welcome1!4@ygcDB","Welcome2!@YGCDB2018#!2")
                print("replace-uat-prd-pwd")
            if("UatDb100" in l):
                l = l.replace("UatDb100","sa")
                print("replace-uat-prd-acc")

            
            # targe
            if("192.168.0.5:3306" in l):
                l = l.replace("192.168.0.5:3306","gz-cdb-4h7ea63v.sql.tencentcdb.com:63851")
                print("t-prd-link")
            if("81.71.32.11:3306" in l):
                l = l.replace("81.71.32.11:3306","gz-cdb-4h7ea63v.sql.tencentcdb.com:63851")
                print("t-prd-link-a")
            if("UAT?" in l):
                l = l.replace("UAT?","PRD?")
                print("t-prd-table")
            
            if("mysql57uat" in l):
                l = l.replace("mysql57uat","dba")
                print("t-prd-acc")
            if("@123456" in l):
                l = l.replace("@123456","WeWPFDE8kBQTBsVg")
                print("t-prd-acc")

            file_content += l
    
    with open(path,"w",encoding=encodingstr,errors='ignore') as f2:
        f2.write(file_content)
        print("-- write")

# def Record(msg, path):
#     with open(path,'a', encoding='utf8') as fr:
#         fr.write(msg)
#         fr.write('\n')



# def CheckContent(sear, path):
#     encodingstr = GetEncode(path)

#     with open(path, "r+", encoding=encodingstr,errors='ignore') as f :
#         for l in f.readlines():
#             if(sear in l):
#                 print('11')
#                 return True
    
#     return False

def GetEncode(path):
    encodestr = "utf-8"
    ftest = open(file=path,mode='rb')
    dr = chardet.detect(ftest.read())
    ftest.close()
    #print(path + " *** " + str(encodestr))
    if(dr['confidence'] == 1.0):
        encodestr = dr['encoding']

    return encodestr


def main():
    print("start...")

    #cur = os.getcwd() + "/LPractice"
    # _txt = curpath + "//r1.txt"
    # if(os.path.isfile(_txt)):
    #     os.remove(_txt)
    
    # SearchPath(g_targetPath, 0)
    #SearchPath(g_targetPath_uat, 1)
    SearchPath(g_targetPath_prd, 2)

    #SearchPath(g_basedata, 0)
    #SearchPath(g_basedata_uat, 0)
    SearchPath(g_basedata_prd, 2)

    print("final done...")

main()